package cn.seaboot.admin.user.web;

import cn.seaboot.admin.mvc.SystemConstant;
import cn.seaboot.admin.mvc.argument.Params;
import cn.seaboot.admin.user.bean.Menu;
import cn.seaboot.admin.user.context.UserContext;
import cn.seaboot.admin.user.service.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 菜单栏编辑
 * <p>@author Mr.css on 2018-6-20
 */
@Controller
@RequestMapping("sys/menu")
@Tag(name = "系统菜单 [t_sys_menu] 管理")
public class MenuCtrl {

    @Resource
    private MenuService menuService;

    @Resource
    private UserContext userContext;

    /**
     * 增
     *
     * @param pid pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "系统菜单 [t_sys_menu] 增")
    @RequestMapping(value = "info", method = RequestMethod.POST)
    public Menu addDir(@Params String pid) {
        return menuService.insert(userContext.getUserDeptId(), pid);
    }

    /**
     * 删
     *
     * @param id id
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "系统菜单 [t_sys_menu] 删")
    @RequestMapping(value = "info", method = RequestMethod.DELETE)
    public int deleteDir(@Params String id) {
        return menuService.deleteById(id);
    }


    /**
     * 改
     *
     * @param dir pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "系统菜单 [t_sys_menu] 改")
    @RequestMapping(value = "info", method = RequestMethod.PUT)
    public int updateDir(@Params String id, Menu dir) {
        return menuService.updateById(dir);
    }

    /**
     * 查询有权限的菜单
     */
    @ResponseBody
    @Operation(description = "查询目前系统现有的全部菜单权限")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public List<Menu> getMenu(
            @RequestParam(defaultValue = SystemConstant.PROJECT_ID) String groupName) {
        return menuService.selectList(groupName);
    }

    /**
     * 查询全部
     */
    @ResponseBody
    @Operation(description = "查询首页菜单")
    @RequestMapping(value = "tree", method = RequestMethod.GET)
    public List<Map<String, Object>> queryMenuTree(
            @RequestParam(defaultValue = SystemConstant.PROJECT_ID) String groupName) {
        return menuService.queryMenuAuthority(groupName, userContext.getUserDept().getRoleId());
    }
}
